package com.biwu.dom4j;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

/**
 * @author 王俊彪
 * @version 1.0
 */
public class DOM4JTest {
    public static void main(String[] args) throws DocumentException {
        //利用SAXReader 加载Demo03.xml
        Document doc = new SAXReader().read("day01_xml_json/Demo05.xml");
        System.out.println(doc);
        //得到根节点
        Element rootElement = doc.getRootElement();
        System.out.println(rootElement.getName());
        //得到根节点下的一级子节点
        List<Element> elements = rootElement.elements("book");
        System.out.println(elements.size());
        //遍历 一级子节点，进行下一步操作
        for (Element childElement : elements) {
            System.out.println(childElement.getName()+" "+
                               childElement.attributeValue("id")+" "+
                                childElement.attributeValue("color")
            );

            //获得一节子节点下面的子元素
            List<Element> elements1 = childElement.elements();
            for (Element childElement1 : elements1) {
                System.out.println(childElement1.getName());
            }
            List<Element> elements2 = elements1.getLast().elements();
            for (Element element : elements2) {
                System.out.println(element.getName()+" "+element.getText());
            }


            System.out.println("------  ----------------------");
        }



    }
}
